export default {
    wanpinghui:function (){
        return (window.wanpinghui || window.webkit) ? true : false;
    },
    loadToken:function (){
        //读取token，未登录时返回空字符串
        return jsCallOcLoadMethod('loadToken')
    },
    loadAPI:function (){
        //读取app的接口地址
        return jsCallOcLoadMethod('loadAPI')
    },
    loadVersion:function (){
        //读取app版本
        return jsCallOcLoadMethod('loadVersion')
    },
    jsCallAppParam:function (type,params){
        if (window.wanpinghui) {
            //Android
            window.wanpinghui.jsCallAppParam(JSON.stringify({type: type, param: params}));
        } else if(window.webkit){
            //IOS
            window.webkit.messageHandlers.jsCallAppParam.postMessage(JSON.stringify({type: type, param: params}));
        }
    }
};


//js 调用 oc 的方法-需要返回值(load 方法咯)
function jsCallOcLoadMethod(methodName){
    if(!methodName) return;
    if (window.wanpinghui && window.wanpinghui[methodName]) {
        return  window.wanpinghui[methodName]();
    }else if(window.webkit){
        //1:先调一下oc的方法
        window.webkit.messageHandlers[methodName].postMessage(null);

        //2:再去本地查找预加载传过来存在本地的值
        let dicStr = localStorage.getItem('wkData-'+methodName);
        return dicStr;
    }
}

function wkLoadMethodCall(methodName){

    if (!validStr(methodName)) {
        return;
    }
    //1:先调一下oc的方法
    window.webkit.messageHandlers[methodName].postMessage(null);

    //2:再去本地查找预加载传过来存在本地的值
    let dicStr = localStorage.getItem('wkData-'+methodName);
    return dicStr;
}
